import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Shapes 1.15
import QtQuick.Layouts 1.15

MainView {
    id: root
    width: 800
    height: 391
    property color btnColor: "#7080C0"
    property int btnHeight: 30
    property int lastViewIndex: 0
    property MainView lastView : null
    property int lastBottomButtonIndex:0
    property int enterBottomButtonIndex:-1
    property int enterViewIndex:-1
    property string enterViewUrl:""
    property string lastViewUrl:""
    property int resetType:-1
    property int deviceID:0
    property int passwordType:0
    signal pwdTrue
    signal pwdFalse
    function checkPwd(){
        var result =false;
        if(passwordType === 0){
            result = mytables.verifyPwd(password1.text);
        }
        else{
            result = password1.text === "254169378"
        }
        password1.text=""
        miniKeyBoard.buttonClean()
        loaderText.visible=false;
        if(result){
            if(resetType>-1){
//                myLoader.visible=false;
//                myLoader.visible=true
                myLoader.setSource(enterViewUrl,{"resetType":resetType,"pwdTrue":1,"deviceID":deviceID})    //哦哦炫一下json格式的参数集。
                //myLoader多次更新，例：先装载EnterPwdPanel.qml，再装载PwdSwitch.qml。
            }
            else{
//                myLoader.visible=false;
//                myLoader.visible=true
                myLoader.setSource(enterViewUrl)
            }
            //destroy()   //哦哦 调试信息：qrc:/EnterPwdPanel.qml:47: Error: Invalid attempt to destroy() an indestructible object 所以应注释掉此行。
        }
        else{
            pwdPanelDialog.title=qsTr("密码错误")
            pwdPanelDialog.open()

        }
    }
    Component.onDestruction: {
        funcTimer.destroy()
    }
    Dialog {    //哦哦 Dialog{}用得真多。
        anchors.centerIn: parent
        modal: true
        id: pwdPanelDialog
        standardButtons: Dialog.Ok
        closePolicy: Popup.CloseOnEscape
        onAccepted: {
            //mainLoader.visible=true;
//            myLoader.visible=false;
//            myLoader.visible=true
            myLoader.source=lastViewUrl //哦哦，回到之前的页面。
            //myLoader.setsourceComponent = Qt.createComponent(lastViewUrl, Component.Asynchronous, rectangle)
//            myLoader.setsourceComponent.onCompleted.connect(function(){
//                mainLoader.visible=false;
//            })
            //mainLoader.visible=false;
        }
        z:100
    }
    Rectangle{
        width:800
        height: 120
        color:"transparent"
            GridLayout{
                anchors.centerIn: parent
                columns: 4
                rows:3

                Button
                {
                    Layout.row:0
                    Layout.column:0
                    Layout.rowSpan: 3
                    Layout.preferredWidth: 100
                    Layout.preferredHeight: btnHeight
                    // x: 152
                    // y: 13
                    text: qsTr("密码")
                    font.pixelSize: 20
                    background: Rectangle{
                        color: btnColor
                            }
                    onClicked: {
//                        miniKeyBoard.lineValue = "";
//                        miniKeyBoard.textInput=password1;
                    }
                }

                Rectangle {
                    id: recOldPwd
                    width:300
                    height:30
                    // x: 218
                    // y: 7
                    color: "transparent"
                    border.width: 0
                    border.color: "white"
                    Layout.row:0
                    Layout.column:1
                    Layout.rowSpan: 3
                    //Layout.preferredWidth: buttonWidth
                    Layout.preferredHeight: 25
                    // 绘制一条边框
                    Shape {
                        anchors.fill: parent
                        ShapePath {
                            strokeWidth: 3
                            strokeColor: "white"
                            strokeStyle: ShapePath.SolidLine
                            startX: 0
                            startY: recOldPwd.height
                            PathLine {
                                x: recOldPwd.width
                                y: recOldPwd.height
                            }
                        }
                    }
                    TextInput{
                        id:password1
                        anchors.fill: parent
                        echoMode: TextInput.Password
                        color:"white"
                        font.pixelSize: 25
                        horizontalAlignment: Text.AlignHCenter
                        MouseArea {
                               anchors.fill: parent
                               onClicked: {
//                                   miniKeyBoard.lineValue = "";
//                                   miniKeyBoard.textInput=password1;
                               }
                           }

                    }
                }

                Button
                {
                    id:btnOK
                    //x: 675
                    //y: 20
                    Layout.row:0
                    Layout.column:2
                    Layout.preferredWidth: 100
                    Layout.preferredHeight: 100
                    Layout.rowSpan: 3
                    //width: 100
                    //height: 100
                    background: Rectangle{
                        border.width: 0
                        color: "#C070B0"
                            }

                    text: qsTr("确定")
                    font.pixelSize: 25
                    palette.buttonText: "white"
                    //Connections {
                    //    target: btnOK
                        onClicked: {
                            if(loaderText.visible)
                            {
                                return;
                            }
                            loaderText.visible=true;
                            funcTimer.start();
                        }
                    //}
                }
                Button
                {
                    id:btnReturn
                    //x: 675
                    //y: 20
                    Layout.row:0
                    Layout.column:3
                    Layout.preferredWidth: 100
                    Layout.preferredHeight: 100
                    Layout.rowSpan: 3
                    //width: 100
                    //height: 100
                    background: Rectangle{
                        border.width: 0
                        color: "#B0B0D0"
                            }

                    text: qsTr("返回")
                    font.pixelSize: 25
                    palette.buttonText: "white"
                    onClicked: {
//                            rectangle.selectedView = lastViewIndex
//                            bottomBar.selected=lastBottomButtonIndex
                        password1.text=""
                        miniKeyBoard.buttonClean()
//                        myLoader.visible=false;
//                        myLoader.visible=true
                        myLoader.source=lastViewUrl
                        //destroy()
                    }
                }
            }
    }
    MiniKeyBoard        //哦哦 自定义空间，用得挺好。
    {
        id: miniKeyBoard
        x: 34
        y: 126
        width:800
        isNumber: false
        textInput: password1
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: enterPwdLoader
        visible:false
        //anchors.verticalCenterOffset: 3
        //anchors.horizontalCenterOffset: 22
    }
    Timer {
        id:funcTimer
                running: false
                repeat: false
                interval:100
                onTriggered: {
                    checkPwd()
                }
            }
}


